﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Common;
using System.Threading;

namespace BLL
{
    public class PassportInfo
    {
        public bool SavePassport(PassportInfoData p)
        {
            Thread.Sleep(500);
            BLL.PassportSerialPort PSP = new BLL.PassportSerialPort((byte)p.Pillar);
            PSP.Display(p.Layer, p.Cell, 2);
            string tx = string.Format("#Save,{0,9},{1,6},{2:yyyy-MM-dd},{3:d2},{4:d2},{5:d3}*sum", p.TaskCode, p.Name, p.Birthday, p.Pillar, p.Layer-1, p.Cell-1);
            PSP.PCDisplay(tx);
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            bool bRet = false;
            PassportInfoData oldP = DAL.PassportInfo.GetInfoByTaskCode(p.TaskCode);
            log.LogContent = string.Format("{0},{1}:柱子{2}层{3}单元格{4}", p.TaskCode, p.Name, p.Pillar, p.Layer-1, p.Cell-1);
            if (oldP != null)
            {
                log.LogType = LogType.护照存取.ToString();
                log.LogContent=string.Format("{0},{1}:柱子{2}层{3}单元格{4},{5},{6} ->", oldP.TaskCode, oldP.Name, oldP.Pillar, oldP.Layer-1, oldP.Cell-1,oldP.Operator,oldP.AddTime)+log.LogContent;
                log.LogRemark = "自动更改位置";
                bRet=DAL.PassportInfo.UpdateInfo(p);
            }
            else
            {
                log.LogType = LogType.护照存取.ToString();
                log.LogRemark = "新增";
                bRet=DAL.PassportInfo.InsertInfo(p);
            }
            if (bRet)          
                log.InserSQLLog();
            else
            {
                log.LogType = LogType.数据库相关.ToString();
                log.LogContent = log.LogRemark + "->" + log.LogContent;
                log.LogRemark = "存入失败";
                log.InserSQLLog();
            }
            return bRet;
        } 
        public bool EditPassport(PassportInfoData p)
        {
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            bool bRet = false;
            PassportInfoData oldP = DAL.PassportInfo.GetInfoByTaskCode(p.TaskCode);
            log.LogContent = string.Format("{0},{1}:柱子{2}层{3}单元格{4}", p.TaskCode, p.Name, p.Pillar, p.Layer-1, p.Cell-1);
            log.LogType = LogType.护照存取.ToString();
            log.LogContent = string.Format("{0},{1}:柱子{2}层{3}单元格{4},{5},{6} ->", oldP.TaskCode, oldP.Name, oldP.Pillar, oldP.Layer-1, oldP.Cell-1, oldP.Operator, oldP.AddTime) + log.LogContent;
            log.LogRemark = "手工编辑";            
            bRet = DAL.PassportInfo.UpdateInfo(p);            
            if (bRet)
                log.InserSQLLog();
            else
            {
                log.LogType = LogType.数据库相关.ToString();
                log.LogContent = log.LogRemark + "->" + log.LogContent;
                log.LogRemark = "手动修改失败";
                log.InserSQLLog();
            }
            return bRet;
        }
        public bool TakeOutPassport(PassportInfoData p)
        {
            Thread.Sleep(500);
            BLL.PassportSerialPort PSP = new BLL.PassportSerialPort((byte)p.Pillar);
            PSP.Display(p.Layer, p.Cell, 4);
            string tx = string.Format("#Out,{0,9},{1,6},{2:yyyy-MM-dd},{3:d2},{4:d2},{5:d3}*sum", p.TaskCode, p.Name, p.Birthday, p.Pillar, p.Layer-1, p.Cell-1);
            PSP.PCDisplay(tx);
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            bool bRet = false;
            PassportInfoData oldP = DAL.PassportInfo.GetInfoByTaskCode(p.TaskCode);
            log.LogContent = string.Format("{0},{1}:柱子{2}层{3}单元格{4},{5},{6}", p.TaskCode, p.Name, p.Pillar, p.Layer-1, p.Cell-1, p.Operator, p.AddTime);
            log.LogRemark = "护照取出";
            log.LogType = LogType.护照存取.ToString();
            bRet = DAL.PassportInfo.DeleteInfoByTaskCode(p.TaskCode);
            if (bRet)
                log.InserSQLLog();
            else
            {
                log.LogType = LogType.数据库相关.ToString();
                log.LogRemark = "删除失败";
                log.InserSQLLog();
            }
            return bRet;
        }
    }
}
